using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZR.Admin.WebApi.Filters;
using ZR.Model.System.Dto;
using ZR.ServiceCore.Services.IService;

namespace ZR.Admin.WebApi.Controllers.System
{
    /// <summary>
    /// 雷达初始化管理
    /// </summary>
    [Verify]
    [Route("system/windLidarInit")]
    [ApiExplorerSettings(GroupName = "sys")]
    public class WindLidarInitController : BaseController
    {
        /// <summary>
        /// 雷达初始化服务
        /// </summary>
        private readonly IWindLidarInitService WindLidarInitService;

        public WindLidarInitController(IWindLidarInitService windLidarInitService)
        {
            WindLidarInitService = windLidarInitService;
        }

        /// <summary>
        /// 执行雷达初始化
        /// </summary>
        /// <param name="commandtype">命令类型</param>
        /// <param name="commanddata">命令数据</param>
        /// <returns></returns>
        [HttpPost("init")]
        public async Task<IActionResult> Init([FromQuery] string commandtype = null, [FromQuery] object commanddata = null)
        {
            try
            {
                var request = new WindLidarInitRequestDto
                {
                    CommandType = "init",
                    CommandData = commanddata
                };
                
                var result = await WindLidarInitService.InitWindLidarAsync(request);
                return SUCCESS(result);
            }
            catch (Exception ex)
            {
                return ToResponse(ApiResult.Error(500, $"雷达初始化失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 执行雷达初始化（公开接口，无需权限）
        /// </summary>
        /// <param name="commandtype">命令类型</param>
        /// <param name="commanddata">命令数据</param>
        /// <returns></returns>
        [HttpPost("public/init")]
        [AllowAnonymous]
        public async Task<IActionResult> PublicInit([FromQuery] string commandtype = "init", [FromQuery] object commanddata = null)
        {
            try
            {
                var request = new WindLidarInitRequestDto
                {
                    CommandType = commandtype,
                    CommandData = commanddata
                };
                
                var result = await WindLidarInitService.InitWindLidarAsync(request);
                return SUCCESS(result);
            }
            catch (Exception ex)
            {
                return ToResponse(ApiResult.Error(500, $"雷达初始化失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 停止采集
        /// </summary>
        /// <returns></returns>
        [HttpPost("stop")]
        public async Task<IActionResult> Stop()
        {
            try
            {
                var result = await WindLidarInitService.StopWindLidarAsync();
                return SUCCESS(result);
            }
            catch (Exception ex)
            {
                return ToResponse(ApiResult.Error(500, $"停止采集失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 开始采集
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <returns></returns>
        [HttpPost("start")]
        public async Task<IActionResult> Start([FromBody] WindLidarStartRequestDto request)
        {
            try
            {
                var result = await WindLidarInitService.StartWindLidarAsync(request);
                return SUCCESS(result);
            }
            catch (Exception ex)
            {
                return ToResponse(ApiResult.Error(500, $"开始采集失败: {ex.Message}"));
            }
        }
    }
}
